﻿/*
=======================================================
    MHCheck【检测报告】
=======================================================
*/

using Entities;

namespace Services
{
    public interface IMHCheckMapping : IMapping<MHCheck, MHCheckModel, MHCheckFilterModel>
    { }

    public class MHCheckMapping : AutoMHCheckMapping, IMHCheckMapping
    {
        private readonly ISqlSugarClient _DB;

        public MHCheckMapping(ICurrentUser currentUser, ISqlSugarClient dB)
            : base(currentUser)
        {
            _DB = dB;
        }

        #region 读数据
        /// <summary>
        /// 读数据，从Entity对象转成Model对象
        /// </summary>
        /// <param name="entity">Entity对象</param>
        /// <returns></returns>
        public override MHCheckModel Entity2Model(MHCheck entity)
        {
            if (entity == null)
            {
                return null;
            }

            MHCheckModel model = base.Entity2Model(entity);

            /***************************************** 下面处理自定义转换 *****************************************/

            return model;
        }
        #endregion

        #region 写数据
        /// <summary>
        /// 写数据，从Model对象转换成Entity对象
        /// </summary>
        /// <param name="model">ViewModel对象</param>
        /// <returns></returns>
        public override MHCheck Model2Entity(MHCheckModel model)
        {
            MHCheck entity = base.Model2Entity(model);

            /***************************************** 下面处理自定义转换 *****************************************/

            return entity;
        }
        #endregion

        #region 读数据(集合)
        /// <summary>
        /// 读数据，从Entity对象转成Model对象
        /// </summary>
        /// <param name="entities">Entity对象</param>
        /// <returns></returns>
        public override List<MHCheckModel> Entity2Model(IEnumerable<MHCheck> entities)
        {
            return entities.Select(x => Entity2Model(x)).ToList();
        }
        #endregion

        #region 写数据(集合)
        /// <summary>
        /// 写数据，从Model对象转换成Entity对象
        /// </summary>
        /// <param name="models">ViewModel对象</param>
        /// <returns></returns>
        public override List<MHCheck> Model2Entity(IEnumerable<MHCheckModel> models)
        {
            return models.Select(x => Model2Entity(x)).ToList();
        }
        #endregion

        #region 搜索条件转换
        /// <summary>
        /// 搜索条件转换，从FilterModel对象转换成Where对象
        /// </summary>
        /// <param name="filterModel">FilterModel对象</param>
        /// <returns></returns>
        public override Expressionable<MHCheck> FilterModel2Where(MHCheckFilterModel filterModel)
        {
            var where = base.FilterModel2Where(filterModel);

            if ((filterModel._DeviceID).HasValue)
            {
                var query = _DB.Queryable<MagnetHead>()
                              .Where(mh => mh.DeviceID == filterModel._DeviceID.Value);

                // 仅当用户类型为集团时添加GroupInfoID条件
                if (_currentUser.CurrentUserInfo.LoginUserType == LoginUserType.集团)
                {
                    query = query.Where(mh => mh.GroupInfoID == _currentUser.CurrentUserInfo.LoginTypeID);
                }

                var mhid = query.Select(mh => mh.MHID).First();
                where = where.And(x => x.MHID == mhid);
            }

            //SearchInput
            if (!string.IsNullOrEmpty(filterModel._SearchInput))
            { }

            return where;
        }
        #endregion
    }
}
